import com.hjk.htimer.HTimer;

import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class Main {

    public static void main(String[] args) throws FileNotFoundException {
        System.setOut(new PrintStream("log.log"));
        HTimer hTimer = HTimer.createHTimer(4,10);
        hTimer.start();
        Random random = new Random();

        for (int i=0;i < 20;i++ ){
            int finalI = i;
            new Thread(()->{
                for (int j=0;j<10000;j++){
                    int period = random.nextInt(20)+1;
                    int delay = random.nextInt(30)+1;
                    boolean flag = random.nextInt(2) == 0;
                    int finalJ = j;
                    final long start = System.currentTimeMillis();
                    String taskName = " hehe_" + (finalI + 1) + "_" + (finalJ + 1);
                    System.out.println(nowTime() + taskName + " delay:" + delay + " period:" + period);
                    hTimer.schedule(hTimer.new TimerTask(){
                        @Override
                        public void work() {
                            long cc = System.currentTimeMillis() - start - (delay*1000);
                            if ( cc > 1000 || cc < -1000){
                                System.out.println(nowTime() + taskName + " he1");
                            } else {
                                System.out.println(nowTime() + taskName + " he2");
                            }

                        }
                    },delay);
                }
            }).start();
        }

        /*for (int i=0; i< 100 ;i++){
            int period = random.nextInt(20)+1;
            int delay = random.nextInt(30)+1;
            boolean flag = random.nextInt(2) == 0;
            System.out.println(nowTime() + " task = [hehe_" + (i+1) + "], delay = [" + delay + "], period = [" + (flag?-1:period) + "]");
            int finalI = i;
            if (flag){
                hTimer.schedule(hTimer.new TimerTask(){
                    @Override
                    public void work() {
                        System.out.println(nowTime() + " hehe_" + (finalI + 1));
                    }
                },delay);
            } else {
                hTimer.schedule(hTimer.new TimerTask(){
                    @Override
                    public void work() {
                        System.out.println(nowTime() + " hehe_" + (finalI + 1));
                    }
                },delay,period);
            }

        }*/
    }

    public static String nowTime(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm:ss.SSS");
        return sdf.format(new Date());
    }
}
